package com.lin.open.server.modules.user.controller;

import com.lin.open.core.response.R;
import com.lin.open.server.modules.user.context.UserLoginContext;
import com.lin.open.server.modules.user.context.UserRegisterContext;
import com.lin.open.server.modules.user.converter.UserConverter;
import com.lin.open.server.modules.user.po.UserLoginPO;
import com.lin.open.server.modules.user.po.UserRegisterPO;
import com.lin.open.server.modules.user.service.UserService;
import com.lin.open.server.modules.user.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Lin
 * @Date: 2023/12/16 11:53:58
 * @description 用户模块控制器
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private UserConverter userConverter;

    @Autowired
    private UserService userService;

    @ApiOperation(
            value = "用户注册接口",
            notes = "该接口提供了用户注册的功能，实现了冥等性注册的逻辑，可以放心多并发调用",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
//    @LoginIgnore
    @PostMapping("register")
    public R register(@Validated @RequestBody UserRegisterPO userRegisterPO) {
        UserRegisterContext userRegisterContext = userConverter.userRegisterPO2UserRegisterContext(userRegisterPO);
        userService.register(userRegisterContext);
        return R.success("注册成功");
    }

    @ApiOperation(
            value = "用户登录接口",
            notes = "该接口提供了用户注册的功能，实现了冥等性注册的逻辑，可以放心多并发调用",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
//    @LoginIgnore
    @PostMapping("doLogin")
    public R register(@Validated @RequestBody UserLoginPO userLoginPO) {
        UserLoginContext context = userConverter.userLoginPo2UserLoginContext(userLoginPO);
        String token = userService.login(context);
        return R.data(token);
    }

    @ApiOperation(
            value = "查询登录用户的基本信息",
            notes = "该接口提供了查询登录用户的基本信息的功能",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @GetMapping("/")
    public R<UserInfoVO> info() {
        UserInfoVO userInfoVO = userService.info();
        return R.data(userInfoVO);
    }
}
